﻿using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;
using System.Configuration;


namespace Liekhus.TimeTracker.Module.Web.Providers
{
    public static class XpoConnectionHelper
    {
        public static IDataLayer GetDataLayer<T>()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            XPDictionary dictionary = new ReflectionDictionary();
            dictionary.GetDataStoreSchema(typeof(T).Assembly);
            IDataStore dataStore = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema);
            return new ThreadSafeDataLayer(dictionary, dataStore);
        }

        public static Session GetSession<T>()
        {
            using (IDataLayer dataLayer = XpoConnectionHelper.GetDataLayer<T>())
            {
                return new Session(dataLayer);
            }
        }
    }
}
